package conf

import (
	"app/internal/common"
	"app/util"
	"fmt"
	"log"
	"path/filepath"

	"github.com/BurntSushi/toml"
)

type AppConf struct {
	common.Net     `toml:"net"`
	common.Redis   `toml:"redis"`
	common.License `toml:"license"`
	common.Appmode `toml:"appmode"`
	common.Monitor `toml:"monitor"`
	common.API     `toml:"api"`
	common.Heart   `toml:"heart"`
	confpath       string
}

func (c *AppConf) GetConfPath() string {
	return c.confpath
}
func (c *AppConf) GetRedisAddr() string {

	addr := fmt.Sprintf("%s:%d", c.Redis.IP, c.Redis.Port)
	return addr
}

func LoadConfig(confpath string) AppConf {

	conf := AppConf{}
	if _, err := toml.DecodeFile(confpath, &conf); err != nil {
		util.Error("config file error")
		log.Fatal(err)
	}
	conf.confpath = filepath.Dir(confpath)
	conf.confpath += "\\"

	return conf
}
